package com.lankystar.sos.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lankystar.sos.dao.ISosOrderDao;
import com.lankystar.sos.dao.IStatusDao;
import com.lankystar.sos.dao.IUserDao;
import com.lankystar.sos.model.SosOrder;
import com.lankystar.sos.model.SosStatus;
import com.lankystar.sos.model.User;
import com.lankystar.sos.model.UserLocation;
import com.lankystar.sos.service.ISosOrderInterface;

@Service
@Transactional
public class SosOrderInterface implements ISosOrderInterface{

	@Autowired
	private ISosOrderDao sosOrderDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IStatusDao statusDao;
	@Override
	public SosOrder addSosOrderMethod(String userPhone,Double longitude, Double latitude) {
		Object[] ob = userDao.selectUserAndUserlocationByUserphone(userPhone);
		
		User user = (User) ob[1];
		SosOrder sos = new SosOrder();
		SosStatus status = statusDao.selectSosStatusBysoskey(0);
		sos.setUser(user);
		sos.setSosStatus(status);
		sos.setCreateTime(new Date());
		sosOrderDao.addSosOrder(sos);
		return sos;
	}
	@Override
	public Map<String, Object> queryUserAndRespMethod(Integer sosId) {
		Map<String, Object> maplist = sosOrderDao.selectUserAndResp(sosId);
		return maplist;
	}
	@Override
	public SosOrder querySosOrderByUserPhoneAndCreateTimeMethod(String userPhone, Date createTime) {
		SosOrder sos = sosOrderDao.selectSosOrderByUserPhoneAndCreateTime(userPhone, createTime); 
		return sos;
	}
	@Override
	public Map<String, Object> querySosOrderBysosIdMethod(Integer sosId) {
		Map<String, Object> map = new HashMap<>();
		Object[] ob = sosOrderDao.selectSosOrderBysosId(sosId);
		SosOrder sosOrder = (SosOrder) ob[0];
		User user = (User) ob[1];
		UserLocation location = (UserLocation) ob[2];
		SosStatus sosStatus = (SosStatus) ob[3];
		map.put("sosId", sosOrder.getSosId());
		map.put("createTime", sosOrder.getCreateTime());
		map.put("userId", user.getUserId());
		map.put("userPhone", user.getUserPhone());
		map.put("userPortrait", user.getUserPortrait());
		map.put("sosvalue", sosStatus.getSosvalue());
		map.put("longitude", location.getLongitude());
		map.put("latitude", location.getLatitude());
		return map;
	}
	@Override
	public List<SosOrder> querySosOrderByuserPhoneAndsosOrderStatusMethod(String userPhone) {
		List<SosOrder> sosOrders = sosOrderDao.selectSosOrderByuserPhoneAndsosOrderStatus(userPhone);
		return sosOrders;
	}

}
